package com.frame.easy.modular.activiti.controller;

import com.frame.easy.base.controller.BaseController;
import com.frame.easy.modular.activiti.model.Historic;
import com.frame.easy.modular.activiti.service.HistoricService;
import com.frame.easy.modular.activiti.service.ProcessTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 流程跟踪
 *
 * @author tengchong
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/auth/activiti/process/trace")
public class ProcessTraceController extends BaseController {
    /**
     * view 路径
     */
    private static final String PREFIX = "modular/activiti/process/trace/";

    @Autowired
    ProcessTraceService service;

    @Autowired
    private HistoricService historicService;

    /**
     * 查看流程实例进度
     */
    @RequestMapping(value = "progress/{executionId}/{processInstanceId}")
    public String progress(Model model, @PathVariable("executionId") String executionId, @PathVariable("processInstanceId") String processInstanceId) {
        model.addAttribute("executionId", executionId);
        model.addAttribute("processInstanceId", processInstanceId);
        model.addAttribute("activityInstances", historicService.select(new Historic(processInstanceId)));
        return PREFIX + "progress";
    }

    /**
     * 查看流程实例进度
     *
     * @param processInstanceId 流程实例ID
     * @param response response
     * @throws IOException ex
     */
    @RequestMapping(value = "process/progress/image/{processInstanceId}")
    public void processProgressImage(@PathVariable("processInstanceId") String processInstanceId, HttpServletResponse response) throws IOException {
        service.readProcessImg(processInstanceId, response);
    }
}
